package cn.appsys.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.pojo.BackUser;
import cn.appsys.service.backend.BackUserService;
import cn.appsys.tools.Constants;


@Controller
@RequestMapping("/manager")
public class BackLoginController {
private Logger logger = Logger.getLogger(BackLoginController.class);
	
	@Resource
	private BackUserService backUserService;
	
	@RequestMapping(value="/dologin",method=RequestMethod.POST)
	public String doLogin(@RequestParam String userCode,@RequestParam String userPassword,HttpServletRequest request,HttpSession session) throws Exception{
		logger.debug("doLogin====================================");
		//调用service方法，进行用户匹配
		BackUser user = backUserService.login(userCode,userPassword);
		if(null != user){//登录成功
			//放入session
			session.setAttribute(Constants.USER_SESSION, user);
			//页面跳转（frame.jsp）
			return "redirect:/manager/sys/main";
		}else{
			//页面跳转（login.jsp）带出提示信息--转发
			request.setAttribute("error", "用户名或密码不正确");
			return "backendlogin";
		}
	}
	
	
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		
		session.removeAttribute(Constants.USER_SESSION);
		return "backendlogin";
	}
	@RequestMapping(value="/sys/main")
	public String main(){
		return "/backend/main";
	}
	
	@RequestMapping(value="/syserror")
	public String sysError(){
		return "syserror";
	}
}
